package org.gurms.web.action.system;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.gurms.common.config.GlobalConfig;
import org.gurms.common.validate.GurmsValid;
import org.gurms.service.dto.system.MenuDto;
import org.gurms.service.system.SysMenuService;
import org.gurms.web.action.BaseAction;
import org.gurms.web.action.CRUDActionSupport;
import org.gurms.web.action.Struts2ActionUtil;
import org.springframework.beans.factory.annotation.Autowired;

@Results({
	@Result(name=BaseAction.RESULT_RELOAD, location="sys-menu", type="redirect")
})
public class SysMenuAction extends CRUDActionSupport<MenuDto> {

	private MenuDto menu = new MenuDto();
	private List<MenuDto> menuList = new ArrayList<MenuDto>();
	
	@Autowired
	private SysMenuService sysMenuService;
	
	public String delete() throws Exception {
		return RESULT_RELOAD;
	}

	@GurmsValid(type=MenuDto.class)
	public String execute() throws Exception {
		menuList = sysMenuService.getList(GlobalConfig.ID_SYS_MENU_ROOT);
		return SUCCESS;
	}

	public String detail() throws Exception {
		// TODO Auto-generated method stub
		return RESULT_DETAIL;
	}
		
	public String save() throws Exception {
		sysMenuService.save(menu);
		return RESULT_RELOAD;
	}

	//---------------------ajax-------------------------------
	public String ajaxSave(){
		try{
			sysMenuService.save(menu);
		}catch(Exception e){
			logger.error("保存用户异常", e);
			page.setSuccess(false);
			page.setReturncode("9999");
			page.setReturnmsg("0000");
		}
		Struts2ActionUtil.renderJson(page);
		return NONE;
	}
	
	public String ajaxDetail(){
		try{
			MenuDto dto = sysMenuService.get(menu);
			page.addResult(dto);
		}catch(Exception e){
			logger.error("查询用户异常", e);
			page.setSuccess(false);
			page.setReturncode("9999");
			page.setReturnmsg("0000");
		}
		Struts2ActionUtil.renderJson(page);
		return NONE;
	}
	
	//----------------------getter/setter---------------------
	public MenuDto getModel() {
		return menu;
	}

	public List<MenuDto> getMenuList() {
		return menuList;
	}

	public void setSysMenuService(SysMenuService menuService) {
		this.sysMenuService = menuService;
	}
}
